<%@page import="java.util.Map"%>
<%@page import="java.util.LinkedHashMap"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Jstl forEach tag demo</title>
</head>
<body>
	<h3>一、九九乘法表</h3>
	<!-- 打印最顶上一行的数字1--9 -->
	<c:forEach var="x" begin="1" end="9" step="1">
		<c:out value="${x}" />
	</c:forEach>
	<br />

	<!-- 双重循环输出九九乘法表的结果 -->
	<!-- var定义一个变量，begin变量的初始值，end变量的结束值,step为步长，就是相当于java的自增（i++）. -->
	<!-- ${i}这个是表示取值，${i*j}这里是算两个数相乘得到的结果，c:out标签就是输出的意思，相当于java中的Syste.out.println() -->
	<c:forEach var="i" begin="1" end="9" step="1">
		<c:forEach var="j" begin="1" end="${i}" step="1">
			<c:out value="${i*j}" />
		</c:forEach>
		<br />
	</c:forEach>

	<%
		String data[] = new String[6];
		data[0] = "hello";
		data[1] = "this";
		data[2] = "is";
		data[3] = "a";
		data[4] = "pen";
		data[5] = "also";
		request.setAttribute("atts", data);
	%>
	<h3>遍历数组1</h3>
	<c:forEach items="${atts}" var="item">
		<c:out value="${item}"></c:out>
	</c:forEach>

	<c:forEach items="${atts}" var="item" varStatus="status">
		<c:if test="${status.index % 3 == 0}">
			<div>start-
		</c:if>
		${status.index}-${item} | 
		<c:if test="${status.index % 3 == 2 && status.last == false}">
			-end</div>
		</c:if>
	</c:forEach>
	end
	</div>

	<h3>遍历数组2</h3>
	<c:forEach items="<%=data%>" var="item">
		<c:out value="${item}"></c:out>
	</c:forEach>

	<h3>遍历action类属性</h3>
	<c:forEach items="${list}" var="item">
		<c:out value="${item}"></c:out>
	</c:forEach>
	<p>
		list size is
		<c:out value="${fn:length(list)}"></c:out>
	</p>
	<table>
		<tr>
			<th>index</th>
			<th>current</th>
			<th>count</th>
			<th>data</th>
		</tr>
		<c:forEach items="${list}" var="item" varStatus="status">
			<tr>
				<td><c:out value="${status.index}" /></td>
				<td><c:out value="${status.current}" /></td>
				<td><c:out value="${status.count}" /></td>
				<td><c:out value="${item}"></c:out></td>
			</tr>
		</c:forEach>
	</table>


	<h3>foreach生成table</h3>
	<table>
		<tr>
			<th>Value</th>
			<th>Square</th>
		</tr>
		<c:forEach var="x" begin="0" end="10" step="2">
			<tr>
				<td><c:out value="${x}" /></td>
				<td><c:out value="${x * x}" /></td>
			</tr>
		</c:forEach>
	</table>

	<c:set var="salary" scope="session" value="${2000*2}" />
	<c:if test="${salary > 2000}">
		<p>
			My salary is:
			<c:out value="${salary}" />
		<p>
	</c:if>

	<c:if test="${salary == 2000}">
		<p>My salary is 2000
		<p>
	</c:if>


	<table>
		<tr>
			<th>index</th>
			<th>count</th>
			<th>current</th>
			<th>item</th>
		</tr>
		<c:forEach var="item" begin="1" end="8" step="1" varStatus="status">
			<tr>
				<td>${status.index}</td>
				<td>${status.count}</td>
				<td>${status.current}</td>
				<td>${item}</td>
			</tr>
		</c:forEach>
	</table>


	<%!public static class TTT {
		private String name;

		public TTT(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}
	}%>
	<%
		Map map = new LinkedHashMap();
		map.put(new TTT("111"), "aaaaaa");
		map.put(new TTT("222"), "bbbbbb");
		request.setAttribute("map", map);
	%>

	<c:forEach items="${map}" var="item">
		${item.key.name}-${item.value}<br />
	</c:forEach>

	<c:forTokens items="a:b:c:d" delims=":" var="token">
		<c:out value="${token}" />
	</c:forTokens>
</body>
</html>